package org.onion.pattern.kit.validation;



import jakarta.validation.groups.Default;
import org.apache.commons.lang3.StringUtils;
import org.onion.pattern.kit.validation.exception.VerifyException;

import java.util.Optional;

/**
 * 验证接口
 */
public interface Verify {

    default Optional<String> verify(String... fields) {
        return this.verify(Default.class, fields);
    }

    default Optional<String> verify(Class<? extends Default> groupClazz, String... fields) {
        String errorMsg = Validators.validate(this, groupClazz, fields);
        if (StringUtils.isNotBlank(errorMsg)) {
            return Optional.of(errorMsg);
        }

        //额外验证
        try {
            errorMsg = this.extraVerify(groupClazz, fields);
        } catch (IllegalArgumentException e) {
            errorMsg = StringUtils.defaultIfBlank(e.getMessage(), "传参异常");
        }
        if (StringUtils.isNotBlank(errorMsg)) {
            return Optional.of(errorMsg);
        }

        return Optional.empty();
    }

    default String extraVerify(Class<? extends Default> groupClazz, String... fields) throws IllegalArgumentException {
        return null;
    }

    default void verifyThrow(String... fields) throws VerifyException {
        Optional<String> verifyOptional = this.verify(fields);
        if (verifyOptional.isPresent()) {
            throw new VerifyException(verifyOptional.get());
        }
    }

    default void verifyThrow(Class<? extends Default> groupClazz, String... fields) throws VerifyException {
        Optional<String> verifyOptional = this.verify(groupClazz, fields);
        if (verifyOptional.isPresent()) {
            throw new VerifyException(verifyOptional.get());
        }
    }


    default void verifyThrowArgumentException(String... fields) throws IllegalArgumentException {
        Optional<String> verifyOptional = this.verify(fields);
        if (verifyOptional.isPresent()) {
            throw new IllegalArgumentException(verifyOptional.get());
        }
    }

    default void verifyThrowArgumentException(Class<? extends Default> groupClazz, String... fields) throws IllegalArgumentException {
        Optional<String> verifyOptional = this.verify(groupClazz, fields);
        if (verifyOptional.isPresent()) {
            throw new IllegalArgumentException(verifyOptional.get());
        }
    }

    default void verifyThrowException(String... fields) throws RuntimeException {
        Optional<String> verifyOptional = this.verify(fields);
        if (verifyOptional.isPresent()) {
            throw new RuntimeException(verifyOptional.get());
        }
    }

    default void verifyThrowException(Class<? extends Default> groupClazz, String... fields) throws RuntimeException {
        Optional<String> verifyOptional = this.verify(groupClazz, fields);
        if (verifyOptional.isPresent()) {
            throw new RuntimeException(verifyOptional.get());
        }
    }


    /**
     * 创建
     */
    interface Insert extends Default {
    }

    /**
     * 删除
     */
    interface Delete extends Default {
    }

    /**
     * 更新
     */
    interface Update extends Default {
    }


    /**
     * 其他
     */
    interface Other extends Default {
    }

}
